<style lang="less">
  @import './login.less';
</style>

<template>
  <div class="login" @keydown.enter="handleSubmit">
    <div class="login-con">
      <Card :bordered="false">
        <p slot="title">
          <Icon type="log-in"></Icon>
          欢迎登录
        </p>
        <div class="form-con">
          <Form ref="loginForm" :model="form" :rules="rules">
            <FormItem prop="username">
              <Input v-model="form.username" placeholder="请输入用户名">
                <span slot="prepend">
                  <Icon :size="16" type="person"></Icon>
                </span>
              </Input>
            </FormItem>
            <FormItem prop="password">
              <Input type="password" v-model="form.password" placeholder="请输入密码">
                <span slot="prepend">
                  <Icon :size="14" type="locked"></Icon>
                </span>
              </Input>
            </FormItem>
            <FormItem>
              <Button @click="handleSubmit" type="primary" long>登录</Button>
            </FormItem>
          </Form>
          <p class="login-tip">输入任意用户名和密码即可</p>
        </div>
      </Card>
    </div>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import http from '../libs/http'

export default {
  data () {
    return {
      form: {
        username: 'developer',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleSubmit () {
      let that = this
      console.log(that.form)
      this.$refs.loginForm.validate((valid) => {
        if (!valid) {
          return false
        }
        http.fetchPost('login', that.form).then(
          res => {
            if (res.code === 0) {
              that.$Message.success(res.msg)
              Cookies.set('userInfo', res.data.userInfo)
              Cookies.set('token', res.data.token)
              var avator = res.data.userInfo.avatar ? res.data.userInfo.avatar : 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg'
              this.$store.commit('setAvatar', avator)
              // sessionStorage.setItem("menuList", JSON.stringify(res.data.menuList))
              this.$router.push({
                name: 'home'
              })
            } else {
              that.$Message.error(res.msg)
            }
          }
        )
      })
    }
  }
}
</script>

<style>

</style>
